package com.app.mingluxing.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.app.mingluxing.R;
import com.app.mingluxing.chat.ChatActivity;

/**
 * 聊天发送图片的adapter
 * Created by Aaron on 16/1/14.
 */
public class PhotoAdapter extends BaseAdapter {

    private Context ctx;

    public PhotoAdapter(Context ctx) {
        this.ctx = ctx;
    }

    private int[] ImageResources = {R.mipmap.ic_choose_photo, R.mipmap.ic_take_photo};

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = new ImageView(ctx);
            viewHolder = new ViewHolder();

            ViewGroup.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            convertView.setLayoutParams(params);
            viewHolder.iv_photo = (ImageView) convertView;
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.iv_photo.setImageResource(ImageResources[position]);

        viewHolder.iv_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (position) {

                    case 0://选择图片

                        if (ctx instanceof ChatActivity) {
                            ((ChatActivity) ctx).choosePhoto();
                        }

                        break;

                    case 1://拍照

                        if (ctx instanceof ChatActivity) {
                            ((ChatActivity) ctx).takePhoto();
                        }

                        break;

                }

            }
        });

        return convertView;
    }

    private class ViewHolder {
        private ImageView iv_photo;
    }

}
